//
//  RWReminderMainCell.m
//  Chat
//
//  Created by 任伟 on 16/3/8.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "RWReminderMainCell.h"

@interface RWReminderMainCell()

@property (nonatomic, strong) UIView  *backGroundView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *markLabel;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation RWReminderMainCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatView];
    }
    return self;
}


- (void)creatView
{
    [self setBackgroundColor:[UIColor clearColor]];
    [self.contentView setBackgroundColor:[UIColor clearColor]];
    
    self.backGroundView = [[UIView alloc]initWithFrame:CGRectMake(10, 8, ScreenWidth - 20, 60)];
    [self.backGroundView.layer setMasksToBounds:YES];
    [self.backGroundView.layer setCornerRadius:5];
    [self.contentView addSubview:self.backGroundView];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, self.backGroundView.width - 100, 30)];
    [self.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [self.titleLabel setTextAlignment:NSTextAlignmentLeft];
    [self.backGroundView addSubview:self.titleLabel];
    
    self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.titleLabel.top + self.titleLabel.height, 0, 20)];
    [self.dateLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [self.dateLabel setTextAlignment:NSTextAlignmentLeft];
    [self.backGroundView addSubview:self.dateLabel];
    
    self.markLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.dateLabel.top + (self.dateLabel.height-13)/2, 0, 13)];
    [self.markLabel setFont:[UIFont boldSystemFontOfSize:9.0f]];
    [self.markLabel.layer setMasksToBounds:YES];
    [self.markLabel.layer setCornerRadius:self.markLabel.height/2];
    [self.markLabel setTextColor:[UIColor whiteColor]];
    [self.markLabel setTextAlignment:NSTextAlignmentCenter];
    [self.backGroundView addSubview:self.markLabel];
    
    self.shareBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.backGroundView.width - 90, 0, 40, RemindCellHeight)];
    [self.shareBtn setImageEdgeInsets:UIEdgeInsetsMake((RemindCellHeight - 20)/2, 10, (RemindCellHeight - 20)/2, 10)];
    self.shareBtn.centerY = self.backGroundView.height/2;
    [self.shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.backGroundView addSubview:self.shareBtn];
    
    self.deleteBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.backGroundView.width - 50, 0, 40, RemindCellHeight)];
    [self.deleteBtn setImageEdgeInsets:UIEdgeInsetsMake((RemindCellHeight - 20)/2, 10, (RemindCellHeight - 20)/2, 10)];
    self.deleteBtn.centerY = self.backGroundView.height/2;
    [self.deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.backGroundView addSubview:self.deleteBtn];
    
}


- (IBAction)shareBtnClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(shareReminderWithModelId:)]) {
        [self.delegate shareReminderWithModelId:self.modelId];
    }
}


- (IBAction)deleteBtnClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(deleteReminderWithModelId:)]) {
        [self.delegate deleteReminderWithModelId:self.modelId];
    }
}

/// 刷新数据
- (void)reloadCellWithModel:(RWReminderDetailModel *)model
{
    self.modelId = model.id;
    if (model.isNotication) {
        [self.backGroundView setBackgroundColor:RGBACOLOR(172, 185, 192, 1)];
        [self.titleLabel setTextColor:RGBACOLOR(226, 226, 226, 1)];
        [self.dateLabel setTextColor:RGBACOLOR(226, 226, 226, 1)];
        [self.markLabel setBackgroundColor:RGBACOLOR(220, 220, 220, 1)];
        [self.shareBtn setImage:[UIImage imageNamed:@"share_reminder1"] forState:UIControlStateNormal];
        [self.deleteBtn setImage:[UIImage imageNamed:@"delete_reminder1"] forState:UIControlStateNormal];
    } else {
        [self.backGroundView setBackgroundColor:RGBACOLOR(234, 249, 254, 1)];
        [self.titleLabel setTextColor:[UIColor blackColor]];
        [self.dateLabel setTextColor:[UIColor blackColor]];
        switch ([model.reminderType integerValue]) {
            case 0:
                [self.markLabel setBackgroundColor:RGBACOLOR(176, 156, 251, 1)];
                break;
            case 1:
                [self.markLabel setBackgroundColor:RGBACOLOR(126, 229, 115, 1)];
                break;
            case 2:
                [self.markLabel setBackgroundColor:RGBACOLOR(113, 203, 241, 1)];
                break;
            case 3:
                [self.markLabel setBackgroundColor:RGBACOLOR(241, 162, 119, 1)];
                break;
                
            default:
                break;
        }
        
        [self.shareBtn setImage:[UIImage imageNamed:@"share_reminder"] forState:UIControlStateNormal];
        [self.deleteBtn setImage:[UIImage imageNamed:@"delete_reminder"] forState:UIControlStateNormal];
    }
    [self.titleLabel setText:model.explain];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate* inputDate = [formatter dateFromString:model.date];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSWeekdayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSSecondCalendarUnit;
    
    comps = [calendar components:unitFlags fromDate:inputDate];
    
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
    [formatter1 setDateFormat:[NSString stringWithFormat:@"M月d日 %@ HH:mm",[self getweek:[comps weekday]]]];
    
    NSString *dateStr = [formatter1 stringFromDate:inputDate];
    self.dateLabel.width = kGetStringWidth(dateStr, [UIFont systemFontOfSize:14.0f], self.backGroundView.width - 100, 20);
    [self.dateLabel setText:dateStr];
    /**
     * 提醒类型
     * 0:重要日子
     * 1:缴费提醒
     * 2:商务提醒
     * 3:自定义
     */
    
    NSString *markStr;
    switch ([model.reminderType integerValue]) {
        case 0:
            markStr = @"重要日子";
            break;
        case 1:
            markStr = @"缴费";
            break;
        case 2:
            markStr = @"商务";
            break;
        case 3:
            markStr = @"自定义";
            break;
            
        default:
            break;
    }
    self.markLabel.left = self.dateLabel.right + 10;
    self.markLabel.width = kGetStringWidth(markStr, [UIFont boldSystemFontOfSize:9.0f], 50, 10) + 13;
    [self.markLabel setText:markStr];
    
}
/**
 *  @creator    w.ping
 *
 *  @function   获取周信息
 *
 *  @param
 *
 *  @return
 */
-(NSString*)getweek:(NSInteger)week
{
    NSString*weekStr=nil;
    if(week==1)
    {
        weekStr=@"周日";
    }else if(week==2){
        weekStr=@"周一";
        
    }else if(week==3){
        weekStr=@"周二";
        
    }else if(week==4){
        weekStr=@"周三";
        
    }else if(week==5){
        weekStr=@"周四";
        
    }else if(week==6){
        weekStr=@"周五";
        
    }else if(week==7){
        weekStr=@"周六";
        
    }
    return weekStr;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
